package com.hnucm.cooklife.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hnucm.cooklife.R;
import com.hnucm.cooklife.base.utils.Constant;
import com.hnucm.cooklife.bean.db.ClasSec;
import com.hnucm.cooklife.bean.home.ClassData;
import com.hnucm.cooklife.ui.activity.DishResultActivity;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ClasAdapter extends BaseSectionQuickAdapter<ClasSec, BaseViewHolder> {

    private Context mContext;


    //此变量类型为自定义接口类型
    private RecomAdapter.onItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(RecomAdapter.onItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    /**
     * 构造方法里， super()必须设置 header layout
     * data可有可无
     */
    public ClasAdapter(int layoutResId, int sectionHeadResId, List<ClasSec> data) {
        super(sectionHeadResId, data);
        // （只有这里有变化）
        // 添加 item 布局、类型
        addItemType(ClasSec.HEADER_TYPE, R.layout.clas_head_item);
        addItemType(ClasSec.NORMAL_TYPE, R.layout.class_item);
    }

    public void setContext(Context context) {
        this.mContext = context;
    }

    @Override
    protected void convertHeader(@NotNull BaseViewHolder baseViewHolder, @NotNull ClasSec clasSec) {
        if (clasSec.getObject() instanceof String) {
            baseViewHolder.setText(R.id.clas_head, (String) clasSec.getObject());
        }
    }


    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, ClasSec clasSec) {
//        Video video = (Video) item.getObject();
        ClassData.DataDTO clas = (ClassData.DataDTO) clasSec.getObject();

        baseViewHolder.setText(R.id.class_name, clas.getClaname());
        Glide.with(mContext)
                .load(clas.getClapic())
                .fitCenter()
                .centerCrop()
                .into((ImageView) baseViewHolder.getView(R.id.class_header));


        baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int itemPosition = getItemPosition(clasSec);
                if(itemPosition != RecyclerView.NO_POSITION){
                    mOnItemClickListener.onSeleClickListener(baseViewHolder.itemView,itemPosition);
                }
            }
        });

//        setOnItemClickListener(new OnItemClickListener() {
//            @Override
//            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
//                Intent intent = new Intent(mContext, DishResultActivity.class);
//                intent.putExtra(Constant.KEY_KEYWORD,clas.getClaname());
//                mContext.startActivity(intent);
//            }
//        });
//        setOnItemClick(baseViewHolder.itemView,getItemPosition(clasSec));
    }



    @Override
    public void onAttachedToRecyclerView(@NotNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if(manager instanceof GridLayoutManager){
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    /**
                     * 头部独占一行
                     * NORMAL网格布局四个一行
                     */
                    int type = getItemViewType(position);
                    return (type == ClasSec.HEADER_TYPE) ? ((GridLayoutManager) manager).getSpanCount() : 1;

                }
            });
        }
    }
}
